public class PTHashbucket {
    static class Node{
        int key;
        int value;
        Node next;

        public Node(int key, int value){
            this.key = key;
            this.value = value;
        }
    }
    public Node[] array=new Node[10];
    public int useSized;
    private static final double LOAD_FACTOR=0.75;
    public void put(int key, int value){
        //1.先获取插入元素的下标
        int index=key% array.length;
        Node cur=array[index];
        //2.判断集合中是否已经包含key
        while(cur!=null){
            if(cur.key==key){
                cur.value=value;
            }
            cur=cur.next;
        }
        //3.插入key
        Node newNode=new Node(key,value);
        newNode.next=array[index];
        array[index]=newNode;
        useSized++;
        //4.判断是否需要扩容
        if(Do_LOAD_FACTOR()>=LOAD_FACTOR){
            resize();
        }
    }

    private void resize() {
        //5.因为数组容量扩大，所以需要重新遍历一遍原来的数组
        Node[] newArray=new Node[array.length*2];
        for(int i=0;i<array.length;i++){
            Node cur=array[i];
            while(cur!=null){
                Node curN=cur.next;
                int index=cur.key% newArray.length;
                cur.next=newArray[index];
                newArray[index]=cur;
                cur=curN;
            }
        }
        array=newArray;
    }

    private double  Do_LOAD_FACTOR(){
        return useSized*1.0 /array.length;
    }

    public int getValue(int key){
        int index=key% array.length;
        Node cur=array[index];
        while(cur!=null){
            if(cur.key==key){
                return cur.value;
            }
            cur=cur.next;
        }
        return -1;
    }
}
